hdu 5459 Jesus Is Here 数学

Jesus Is Here

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5459

Description

I've sent Fang Fang around 201314 text messages in almost 5 years. Why can't she make sense of what I mean?
``But Jesus is here!" the priest intoned. ``Show me your messages."
Fine, the first message is s1=‘‘c" and the second one is s2=‘‘ff".
The i-th message is si=si−2+si−1 afterwards. Let me give you some examples.
s3=‘‘cff", s4=‘‘ffcff" and s5=‘‘cffffcff".

``I found the i-th message's utterly charming," Jesus said.
``Look at the fifth message". s5=‘‘cffffcff" and two ‘‘cff" appear in it.
The distance between the first ‘‘cff" and the second one we said, is 5.
``You are right, my friend," Jesus said. ``Love is patient, love is kind.
It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.
Love does not delight in evil but rejoices with the truth.
It always protects, always trusts, always hopes, always perseveres."

Listen - look at him in the eye. I will find you, and count the sum of distance between each two different ‘‘cff" as substrings of the message.

Input

An integer T (1≤T≤100), indicating there are T test cases.
Following T lines, each line contain an integer n (3≤n≤201314), as the identifier of message.

Output

The output contains exactly T lines.
Each line contains an integer equaling to:

∑i<j:sn[i..i+2]=sn[j..j+2]=‘‘cff"(j−i) mod 530600414,

where sn as a string corresponding to the n-th message.

Sample Input

9
5
6
7
8
113
1205
199312
199401
201314

Sample Output

Case #1: 3
Case #2: 2
Case #3: 2
Case #4: -1
Case #5: 2
Case #6: 4
Case #7: 1
Case #8: -1

HINT

 

题意

f1=c,f2=ff,fn = fn-1+fn-2

然后问你每对cff之间的距离和加起来是多少

题解:

暴力找规律,然后类似fib数列一样递推

不断归纳就好了……

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N=250000;
const long long pr=530600414;
long long  sum[N],pre[N],tot[N],suf[N],s[N],f[N];

int main()
{
    s[5]=3LL;s[6]=4LL;
    pre[5]=5LL;pre[6]=8LL;
    sum[5]=5LL;sum[6]=11LL;
    suf[5]=5LL;suf[6]=8LL;
    tot[5]=5LL;tot[6]=13LL;
    f[5]=5LL;f[6]=16LL;
    for(int i=7;i<N;i++)
    {
        s[i]=(s[i-1]+s[i-2]-1LL)%pr;
        if(i&1)
        {
            pre[i]=(pre[i-2]+pre[i-1]+5)%pr;
            suf[i]=(suf[i-2]+suf[i-1]+5)%pr;
            sum[i]=(sum[i-2]+(s[i-1]-1)*(pre[i-2]+5)+sum[i-1])%pr;
            tot[i]=(tot[i-1]+(s[i-2]-1)*(suf[i-1]+5)+tot[i-2])%pr;
            f[i]=(f[i-2]+f[i-1]+(s[i-1]-1)*(s[i-2]-1)*5+tot[i-2]*(s[i-1]-1)+sum[i-1]*(s[i-2]-1))%pr;
        }
        else
        {
            pre[i]=(pre[i-2]+pre[i-1]+3)%pr;
            suf[i]=(suf[i-2]+suf[i-1]+3)%pr;
            sum[i]=(sum[i-2]+(s[i-1]-1)*(pre[i-2]+3)+sum[i-1])%pr;
            tot[i]=(tot[i-1]+(s[i-2]-1)*(suf[i-1]+3)+tot[i-2])%pr;
            f[i]=(f[i-2]+f[i-1]+(s[i-1]-1)*(s[i-2]-1)*3+tot[i-2]*(s[i-1]-1)+sum[i-1]*(s[i-2]-1))%pr;
        }
       // cout<<i<<" "<<s[i]<<" "<<pre[i]<<" "<<sum[i]<<" "<<tot[i]<<" "<<f[i]<<endl;
    }
    int T;
    long long n;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        scanf("%d",&n);
        printf("Case #%d: %I64d\n",cas,f[n]);
    }
}

 

posted @ 2015-09-19 23:02  qscqesze  阅读(387)  评论(0编辑  收藏  举报